﻿@model PollModel
@using Nop.Web.Models.Polls;
<div class="poll" id="poll-block-@(Model.Id)">
    
    @Model.Name

    @if (Model.AlreadyVoted)
    {
        <ol class="poll-results">
            @foreach (var item in Model.Answers)
            {
                <li class="answer">
                    @string.Format(T("Polls.VotesResultLine").Text, item.Name, item.NumberOfVotes, item.PercentOfTotalVotes.ToString("0.0"))
                </li>
            }
        </ol>
        <span class="poll-total-votes">@string.Format(T("Polls.TotalVotes").Text, Model.TotalVotes)</span>
    }
    else
    {
        <ul class="poll-options">
            @foreach (var item in Model.Answers)
            {
                <li class="answer">
                    <input id="pollanswers-@(item.Id)" type="radio" name="pollanswers-@(Model.Id)" value="@item.Id" />
                    <label for="pollanswers-@(item.Id)">@item.Name</label>
                </li>
            }
        </ul>
        <div class="buttons pull-text-right">
            <input type="button" value="@T("Polls.Vote")" id="vote-poll-@(Model.Id)" class="vote-poll-button btn btn-primary" />
            <span id="poll-voting-progress-@(Model.Id)" style="display: none;" class="please-wait">@T("Common.Wait...")</span>
        </div>
        <div class="poll-vote-error" id="block-poll-vote-error-@(Model.Id)">
        </div>
        
        <script type="text/javascript">
            $(document).ready(function () {
                $('#vote-poll-@(Model.Id)').click(function () {
                var pollAnswerId = $("input:radio[name=pollanswers-@(Model.Id)]:checked").val();
                if (typeof (pollAnswerId) == 'undefined') {
                    alert('@Html.Raw(HttpUtility.JavaScriptStringEncode(T("Polls.SelectAnswer").Text))');
                }
                else {
                    var voteProgress = $("#poll-voting-progress-@(Model.Id)");
                    voteProgress.show();
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: "@(Url.RouteUrl("PollVote"))",
                        data: { "pollAnswerId": pollAnswerId },
                        success: function (data) {
                            voteProgress.hide();

                            if (data.error) {
                                $("#block-poll-vote-error-@(Model.Id)").html(data.error);
                                $('#block-poll-vote-error-@(Model.Id)').fadeIn("slow").delay(2000).fadeOut("slow");
                            }

                            if (data.html) {
                                $("#poll-block-@(Model.Id)").replaceWith(data.html);
                            }
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Failed to vote.');
                            voteProgress.hide();
                        }
                    });
                }
                return false;
            });
        });
        </script>
    }
</div>
